﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Windows.Forms;
using XCI.Core;
using XCI.Helper;
using XCI.Sys.Helper;
using XCI.Sys.Service;
using XCI.Windows.Forms;
using XCI.Windows.Helper;

namespace XCI.Sys.Forms
{
    public partial class FrmCaptcha : FrmBase
    {
        public FrmCaptcha(string _account)
        {
            InitializeComponent();
            Account = _account;
        }

        private bool IsExit { get; set; }
        private string Account { get; set; }
        private int count = 0;

        /// <summary>
        /// 输入的验证码
        /// </summary>
        public string Captcha { get; set; }

        private void btnUnLock_Click(object sender, EventArgs e)
        {
            Captcha = editCaptcha.Text;
            if (string.IsNullOrEmpty(Captcha))
            {
                MessageBoxHelper.ShowWarn("请输入验证码");
                editCaptcha.Select();
                return;
            }

            IsExit = true;
            this.DialogResult = DialogResult.OK;
        }

        private void Build()
        {
            if (count > 10)
            {
                MessageBoxHelper.ShowError("请勿恶意刷新");
                return;
            }
            var bytes = UserService.Instance.Captcha(Account);
            var img = ImageHelper.FromArray(bytes);
            pictureBox1.Image = img;
            count++;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            AppHelper.KillApplication();
        }

        private void UnLockMainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = !IsExit;
        }

        private void FrmCaptcha_Load(object sender, EventArgs e)
        {
            Build();
            editCaptcha.Width = pictureBox1.Width;
            SplashHelper.CloseProgressPanel();
            NativeMethods.SetForegroundWindow(Handle);
        }

        private void EditPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                btnUnLock.PerformClick();
            }
        }

        private void PictureBox1_Click(object sender, EventArgs e)
        {
            Build();
        }
    }
}